﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using CNotebook;

namespace NotebookS2
{
    public partial class CustomerInfo : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public CustomerInfo()
        {
            InitializeComponent();
        }

        public string ActiveUserID { get; set; }
        public string ConnectionString { get; set; }

        public void ShowSearch()
        {
            FND_CUSTINFO oForm = new FND_CUSTINFO();

            oForm.BusinessPartnerType = "Customer";
            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            if (oForm.CardCode != null || oForm.CardCode.Trim() != "")
            {
                txtCode.Text = oForm.CardCode;
                txtName.Text = oForm.CardName;

                txtTIN.Text = oForm.TIN;
                txtUnitNo.Text = oForm.UnitNo;
                txtStreetAddress.Text = oForm.Street;
                txtBarangayOrDistrict.Text = oForm.Baranggay;
                txtCityOrMunicipality.Text = oForm.CityOrMunicipality;
                txtPostalCode.Text = oForm.PostalCode;

                txtProvinceOrMetro.Text = oForm.ProvinceOrMetro;
                txtPhone.Text = oForm.Phone;
                txtFax.Text = oForm.Fax;
                txtContactPerson.Text = oForm.ContactPerson;

                btnAdd.Text = "Update";
                txtCode.Enabled = false;
            }
        }

        private void CustomerInfo_Load(object sender, EventArgs e)
        {
            cboBPType.SelectedIndex = 1;
            cboBPType.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                CustomerInfoManager oManager = new CustomerInfoManager();
                CustomerInfoUnit oUnit = new CustomerInfoUnit();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oUnit.CardCode = txtCode.Text;
                oUnit.CardName = txtName.Text;
                oUnit.Type = "Customer";
                oUnit.TIN = txtTIN.Text;

                oUnit.UnitNo = txtUnitNo.Text;
                oUnit.Street = txtStreetAddress.Text;
                oUnit.Barangay = txtBarangayOrDistrict.Text;
                oUnit.CityOrMunicipality = txtCityOrMunicipality.Text;
                oUnit.PostalCode = txtPostalCode.Text;
                oUnit.ProvinceOrMetro = txtProvinceOrMetro.Text;

                oUnit.ContactPerson = txtContactPerson.Text;
                oUnit.Phone = txtPhone.Text;
                oUnit.Fax = txtFax.Text;

                oManager.AddNewCustomer(oUnit);

                oManager.Close();

                txtCode.Enabled = false;

                btnAdd.Text = "Update";

                MessageBox.Show("Customer information is successfully added to the record!!!", "Customer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                CustomerInfoManager oManager = new CustomerInfoManager();
                CustomerInfoUnit oUnit = new CustomerInfoUnit();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oUnit.CardCode = txtCode.Text;
                oUnit.CardName = txtName.Text;
                oUnit.Type = "Customer";
                oUnit.TIN = txtTIN.Text;

                oUnit.UnitNo = txtUnitNo.Text;
                oUnit.Street = txtStreetAddress.Text;
                oUnit.Barangay = txtBarangayOrDistrict.Text;
                oUnit.CityOrMunicipality = txtCityOrMunicipality.Text;
                oUnit.PostalCode = txtPostalCode.Text;
                oUnit.ProvinceOrMetro = txtProvinceOrMetro.Text;

                oUnit.ContactPerson = txtContactPerson.Text;
                oUnit.Phone = txtPhone.Text;
                oUnit.Fax = txtFax.Text;

                
                oManager.UpdateCustomerInfo(oUnit);

                oManager.Close();

                txtCode.Enabled = false;

                btnAdd.Text = "Update";

                MessageBox.Show("Customer information is successfully updated!!!", "Customer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}